package org.delphy.indy2.layers
{
	import com.degrafa.paint.GradientStop;
	import com.degrafa.skins.GraphicProgrammaticSkin;
	
	import flash.display.DisplayObject;
	
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;
	import mx.skins.Border;
	import mx.core.UIComponent;
	
	import org.delphy.indy2.core.ICarbonComponent;
	
	public class GradientStopFactory
	{
		/**
		 * @public
		 * Get a collection of GradientStops from CSS selector specified by styleName, and CSS property specified by prop
		 * prop MUST be formatted in the following pair:
		 * 	propColors: #..., #..., ...
		 * 	propAlphas: ..., ..., ...	(optional)
		 * 	propRatio: ..., ..., ... (optional)
		 * NOTE:
		 * 	Currently, only two ratio units are supported, namely "pixels" and "percentage". If propRatio is in "0.x" format, then we treat it as a percentage,
		 * if not, we will automatically treat it as in pixels. To avoid such conventions, we need to manually add your GradientStops.
		 * 
		 * Color is a required property for each GradientStop, while alpha and ratio is optional. If an overlap exists, then a overlapped value
		 * is prefered. 
		 */
		public static function createGradientStops(host:*, prop:String, 
													 colorOverride:Array=null,
													 alphaOverride:Array=null,
													 ratioOverride:Array=null,
													 ratioUnitOverride:Array=null):Array{
			
			var colors:Array;
			var alphas:Array;
			var ratios:Array;
			var ratioUnits:Array;
			
			var defaultStyle:CSSStyleDeclaration;
			if(host is Border)
				defaultStyle=StyleManager.getStyleDeclaration("."+ICarbonComponent(host.styleName).defaultStyleName);
			else
				defaultStyle=StyleManager.getStyleDeclaration("."+ICarbonComponent(host).defaultStyleName);
			
			colors = host.getStyle(prop+"Colors") ? host.getStyle(prop+"Colors") : defaultStyle.getStyle(prop+"Colors");
			alphas = host.getStyle(prop+"Alphas") ? host.getStyle(prop+"Alphas") : defaultStyle.getStyle(prop+"Alphas");
			ratios = host.getStyle(prop+"Ratios") ? host.getStyle(prop+"Ratios") : defaultStyle.getStyle(prop+"Ratios");
			ratioUnits = host.getStyle(prop+"RatioUnits") ? host.getStyle(prop+"RatioUnits") : defaultStyle.getStyle(prop+"RatioUnits");
			
			// User defined values may override the default CSS styles
			colors = colorOverride ? colorOverride : colors;
			alphas = alphaOverride ? alphaOverride : alphas;
			ratios = ratioOverride ? ratioOverride : ratios;
			ratioUnits = ratioUnitOverride ? ratioUnitOverride : ratioUnits;
			
			var colorsLength:Number= colors ? colors.length : 0;
			var alphasLength:Number= alphas ? alphas.length : 0;
			var ratiosLength:Number= ratios ? ratios.length : 0;
			var ratioUnitsLength:Number = ratioUnits ? ratioUnits.length : 0;
			var len:uint=Math.max(colorsLength, alphasLength, ratiosLength, ratioUnitsLength);
			
			var gradientCollection:Array=new Array();
			
			for(var i:uint=0;i<len;i++){
				var color:Number=NaN;
				var alpha:Number=NaN;
				var ratio:Number=NaN;
				var ratioUnit:String=null;
				
				if(colors)
					color=colors[i];
				if(alphas)
					alpha=alphas[i];
				if(ratios)
					ratio=ratios[i];
				if(ratioUnits)
					ratioUnit=ratioUnits[i];
				
				var gradientStop:GradientStop=new GradientStop();
				
				// Setup color
				if(!isNaN(color))
					gradientStop.color=color;
				
				// Setup alpha
				if(!isNaN(alpha))
					gradientStop.alpha=alpha;
				
				// Setup ratio
				if(!isNaN(ratio))
					gradientStop.ratio=ratio;
				
				// Setup ratioUnit
				if(ratioUnit!=null)
					gradientStop.ratioUnit=ratioUnit;
				else	
					gradientStop.ratioUnit= gradientStop.alpha<=1 ? "ratio" : "pixels";
				
				gradientCollection.push(gradientStop);	
			}
			
			return gradientCollection; 

		}
		
		/**
		 * @public
		 * Create a GradientStops array using explict values
		 */
		public static function createGradientStopsManually(colors:Array=null, alphas:Array=null, 
												   ratios:Array=null, ratioUnits:Array=null):Array{
			var colorsLength:Number= colors ? colors.length : 0;
			var alphasLength:Number= alphas ? alphas.length : 0;
			var ratiosLength:Number= ratios ? ratios.length : 0;
			var ratioUnitsLength:Number = ratioUnits ? ratioUnits.length : 0;
			var len:uint=Math.max(colorsLength, alphasLength, ratiosLength, ratioUnitsLength);
			
			var gradientCollection:Array=new Array();

			for(var i:uint=0;i<len;i++){
				// Try to read from array defined in CSS
				var color:Number=NaN;
				var alpha:Number=NaN;
				var ratio:Number=NaN;
				var ratioUnit:String=null;
				
				if(colors)
					color=colors[i];
				if(alphas)
					alpha=alphas[i];
				if(ratios)
					ratio=ratios[i];
				if(ratioUnits)
					ratioUnit=ratioUnits[i];
				
				var gradientStop:GradientStop=new GradientStop();
				
				// Setup color
				if(color)
					gradientStop.color=color;
				
				// Setup alpha
				if(alpha)
					gradientStop.alpha=alpha;
				
				// Setup ratio
				if(ratio)
					gradientStop.ratio=ratio;
				
				// Setup ratioUnit
				if(ratioUnit)
					gradientStop.ratioUnit=ratioUnit;
				else	
					gradientStop.ratioUnit= gradientStop.alpha<=1 ? "ratio" : "pixels";
				
				gradientCollection.push(gradientStop);	
			}
			
			return gradientCollection; 
		}
		
	}
}